﻿package lz.controls
{
	import com.common.utils.Reflection;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import lz.controls.events.CloseEvent;

	/**
	 *提示框 
	 * @author lake
	 */	
	public class oAlert extends BaseWindow
	{
		private var symbol 			:MovieClip;
		public var _Content				:TextField;
		public var _Yes					:MovieClip;
		public var _No					:SimpleButton;
		public var closeBtn 			:SimpleButton;
		public var txtTime				:TextField;
		private var _yesEnabled		:Boolean = true;
		private var _noEnabled			:Boolean = true;
		private var _cancelEnabled		:Boolean = true;
		private var _firstButton		:uint = 1;
		private var _parent			:Sprite;
		private var _closeHander		:Function;
		private var _timeOut			:int = 0;
		private var timer 				:Timer;
		
		public function oAlert() 
		{
			symbol = Reflection.createInstance("com.liangzhong.ui.oAlert");
			this.addChild(symbol);
			windowBg = symbol.windowBg;
			configUI();
			dragEnabled = true;
			_Content = symbol._Content;
			_Yes = symbol._Yes;
			_No = symbol._No;
			closeBtn = symbol.closeBtn;
			settleButton(_Yes);
			settleButton(_No);
			var yesBtn :LButton = new LButton(_Yes);
			yesBtn.enable = true;
			yesBtn.buttonMode = true;
			_Content.mouseEnabled = false;
			closeBtn.addEventListener(MouseEvent.CLICK,onClose);
			txtTime = symbol.txtTime;
			//show("秋限制:玩家金额大于等于庄上金额的7倍,在社会主义的中国，共同富裕是我们的目标，有木有",3);
		}
		/**
		 *显示信息 
		 * @param txtContent
		 * @param mode
		 * @param callBack
		 */			
		public function show(txtContent:String, 
							 mode:uint = Alert.YES, 
							 callBack:Function = null) : void
		{
			formatText(txtContent);
			_closeHander = callBack;
			switch(mode)
			{
				case Alert.YES:
				{
					yesVisible = true;
					noVisible = false;
					_firstButton = Alert.YES;
					_Yes.x = (width - _Yes.width)/2;
					break;
				}
				case Alert.NO:
				{
					noVisible = true;
					_firstButton = Alert.NO;
					break;
				}
				case (Alert.YES | Alert.NO):
				{
					yesVisible = true;
					noVisible = true;
					_Yes.x = 73;
					_No.x = 186;
					_firstButton = Alert.YES;
					break;
				}
				default:
				{
					
				}
			}
		}
		/**
		 *format 
		 * @param txt
		 * 
		 */		
		private function formatText(txt:String) : void
		{
			_Content.htmlText = txt;
			if (_Content.numLines > 1)
			{
				_Content.y = 58;
				_Content.height = (_Content.numLines) * 25;
				//_Content.y += (130 - _Content.height)/2;
			}
			else
			{
				_Content.y = 81;
				_Content.height = 25;
			}
		}
		/**
		 *hide 
		 * 
		 */		
		public function hide() : void
		{
			stopTimer();
			if (_parent)
			{
				_parent.graphics.clear();
			}
			if (parent)
			{
				parent.removeChild(this);
			}
			_yesEnabled = true;
			_noEnabled = true;
			_cancelEnabled = true;
			
		}
		/**
		 * add event
		 * @param btn
		 * 
		 */		
		private function settleButton(btn :DisplayObject) : void
		{
			btn.visible = true;
			btn.addEventListener(MouseEvent.CLICK, onClickHandler);
			
		}
		/**
		 *click button handler 
		 * @param event
		 * 
		 */		
		private function onClickHandler(event:MouseEvent) : void
		{
			var mode : int = Alert.YES;
			if (event.target == _No)
			{
				mode = Alert.NO;
			}
			hide();
			if (_closeHander is Function)
			{
				_closeHander(mode);
			}
			this.dispatchEvent(new CloseEvent(CloseEvent.CLOSE,false,false,mode));
		}
		/**
		 *close handler 
		 * @param e
		 * 
		 */		
		private function onClose(e :MouseEvent):void
		{
			hide();
			if (_closeHander is Function)
			{
				_closeHander(Alert.NO);
			}
			this.dispatchEvent(new CloseEvent(CloseEvent.CLOSE,false,false,Alert.NO));
		}
		/**
		 * yesVisible
		 * @param value
		 * 
		 */		
		private function set yesVisible(value:Boolean) : void
		{
			_Yes.visible = value;
			
		}
		
		private function set noVisible(value:Boolean) : void
		{
			_No.visible = value;
			
		}
		
		public function defaultCloseEvent() : void
		{
			hide();
			if (_closeHander is Function)
			{
				_closeHander(_firstButton);
			}
		}
		
		public function get content() : Sprite
		{
			return this;
		}
		
		public function set oParent(value:Sprite) : void
		{
			_parent = value;
			
		}
		/**
		 *设置确认按钮是否激活
		 * @param value
		 * 
		 */		
		public function set yesEnabled(value:Boolean) : void
		{
			_yesEnabled = value;
		}
		/**
		 *设置取消按钮是否激活
		 * @param value
		 * 
		 */		
		public function set noEnabled(value:Boolean) : void
		{
			_noEnabled = value;
		}
		/**
		 *设置取消按钮是否激活 
		 * @param value
		 * 
		 */		
		public function set cancelEnabled(value:Boolean) : void
		{
			_cancelEnabled = value;
		}
		/**
		 * 设置yes按钮标签
		 * @param value
		 * 
		 */		
		public function set yesLabel(value :String):void
		{
			if(_Yes && _Yes.txtLabel)
			{
				_Yes.txtLabel.htmlText = value + "";
			}
		}
		/**
		 *set timeOut 
		 * @param value
		 * 
		 */		
		public function set timeOut(value :int):void
		{
			_timeOut = value;
			if(_timeOut > 0)
			{
				timer = new Timer(1000,_timeOut);
				timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerCompleteHandler);
				timer.start();
				if(txtTime)
				{
					txtTime.text = "" + _timeOut;
				}
			}
		}
		/**
		 * onTimerHandler
		 * @param e
		 * 
		 */		
		private function onTimerHandler(e :TimerEvent):void
		{
			_timeOut --;
			if(txtTime)
			{
				txtTime.text = "" + _timeOut;
			}
		}
		/**
		 * onTimerCompleteHandler
		 * @param e
		 * 
		 */		
		private function onTimerCompleteHandler(e :TimerEvent):void
		{
			hide();
			if (_closeHander is Function)
			{
				_closeHander(Alert.NO);
			}
			this.dispatchEvent(new CloseEvent(CloseEvent.CLOSE,false,false,Alert.NO));
		}
		
		private function stopTimer():void
		{
			if(timer)
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER,onTimerHandler);
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE,onTimerCompleteHandler);
			}
		}
		
		public function get timeOut():int
		{
			return _timeOut;
		}
	}
}
